<?php 
session_start();
if (isset($_POST['urd'])){
    include_once("../c/config.php");
    require_once "../jssdk.php";
    /**
            酸菜鱼汤饺立减2元   pe2K2ju_Es0puZxEldrf9EJBMUOA
            新春5元代金券  pe2K2jo9fYi89Ly53FyEHxoNkeaw
            酸菜鱼汤饺免费品尝  pe2K2jk5L5pL2b8qrOd5oxtQudFU
     */
    function httpGet($url){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_URL, $url);
        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }
    $mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
    if (!$mysqli->set_charset("utf8")) {printf("Error loading character set utf8: %s\n", $mysqli->error);}
    
    $urd=trim($_POST['urd']);
    $theday=date("Y-m-d");
    
    
    $jId=4;
    $jName=array(4=>'xy', 3=>'fq', 2=>'chihuo',1=>'ddjlomg'); 
    $query=$mysqli->query("select id from play where openid='".$_SESSION['openid']."' and jid!='4' limit 0, 1");
    if($query->num_rows>0){
        
    }else{
        $mysqli->query("insert into play_log(openid,createtime) values('".$_SESSION['openid']."','".date("Y-m-d H:i:s")."')  ");
        $query=$mysqli->query("select id from play_log where openid='".$_SESSION['openid']."' order by id desc limit 0, 1");
        if($query->num_rows>0){
            $getPlay=$query->fetch_array();
            $playId=$getPlay['id'];
            $query=$mysqli->query("select name from chi where theid='".$playId."' limit 0, 1");
            if($query->num_rows>0){
                $chiInfo=$query->fetch_array();
                $jId=$chiInfo['name'];
            }
        }
        
        //oe2K2jjniX57VFTjLyP5wO7G3-to
        /**    
        //if('oe2K2jjniX57VFTjLyP5wO7G3-to' == $_SESSION['openid']){
        $query=$mysqli->query("select province from users where openid='".$_SESSION['openid']."' limit 0, 1");
            if($query->num_rows>0){
                $getCityInfo=$query->fetch_array();
                $city=$getCityInfo['province'];
                if('上海' == $city){
                    $mysqli->query("insert into city_log(openid,createtime) values('".$_SESSION['openid']."','".date("Y-m-d H:i:s")."')  ");
                    $queryCityLog=$mysqli->query("select id from city_log where openid='".$_SESSION['openid']."' order by id desc limit 0, 1");
                    $getCityId=$queryCityLog->fetch_array();
                    $cityId=$getCityId['id'];
                    if(2 == $cityId){
                        $jId=1;
                    }
                }
            }
            
         //}   
         */
       
         
        
    }
  
    
    //$jId=2;
    //4.幸运奖 (酸菜鱼汤饺立减2元) 3.免单奖 (酸菜鱼汤饺免费品尝) 2.吃货奖 (新春5元代金券) 1.喜奖
    
    $cardId='pe2K2ju_Es0puZxEldrf9EJBMUOA';
    if(3 == $jId){
        $cardId='pe2K2jk5L5pL2b8qrOd5oxtQudFU';
    }elseif (2 == $jId) {
        $cardId='pe2K2jo9fYi89Ly53FyEHxoNkeaw';
    }elseif (1 == $jId) {
        $cardId='';
    }else{
        $cardId='pe2K2ju_Es0puZxEldrf9EJBMUOA';
    }
    
    
    
    $openid=$_SESSION['openid'];
    
    $sign='';
    
    $code='';
    if (4 == $jId){  
        $mysqli->query("insert into juan2_log(openid,createtime) values('".$openid."','".date("Y-m-d H:i:s")."')  ");
        $queryJuan=$mysqli->query("select id from juan2_log where openid='".$openid."' order by id desc  limit 0, 1");
        $getJuanInfo=$queryJuan->fetch_array();
        $getJuanId=$getJuanInfo['id'];
        $queryCode=$mysqli->query("select name from juan2 where id='".$getJuanId."' limit 0, 1");
        $juanCodeInfo=$queryCode->fetch_array();
        $code=$juanCodeInfo['name'];
    }
    if (3 == $jId){
        $mysqli->query("insert into mian_log(openid,createtime) values('".$openid."','".date("Y-m-d H:i:s")."')  ");
        $queryJuan=$mysqli->query("select id from mian_log where openid='".$openid."' order by id desc  limit 0, 1");
        $getJuanInfo=$queryJuan->fetch_array();
        $getJuanId=$getJuanInfo['id'];
        $queryCode=$mysqli->query("select name from mian where id='".$getJuanId."' limit 0, 1");
        $juanCodeInfo=$queryCode->fetch_array();
        $code=$juanCodeInfo['name'];
    }
    
    if (2 == $jId){
        
        
        $mysqli->query("insert into juan5_log(openid,createtime) values('".$openid."','".date("Y-m-d H:i:s")."')  ");
        $queryJuan=$mysqli->query("select id from juan5_log where openid='".$openid."' order by id desc  limit 0, 1");
        $getJuanInfo=$queryJuan->fetch_array();
        $getJuanId=$getJuanInfo['id'];
        $queryCode=$mysqli->query("select name1,name2,name3,name4 from juan5 where id='".$getJuanId."' limit 0, 1");
        $juanCodeInfo=$queryCode->fetch_array();
        $code=$juanCodeInfo['name1'];
        $code2=$juanCodeInfo['name2'];
        $code3=$juanCodeInfo['name3'];
        $code4=$juanCodeInfo['name4'];
        
        
        $sql = "insert into play(thecode2,thecode3,thecode4,openid,thecode,jid,tiao,theday,createtime) values ('".$code2."','".$code3."','".$code4."','".$_SESSION['openid']."','".$code."','".$jId."','".$urd."','".$theday."','".date("Y-m-d H:i:s")."' )";
        $mysqli->query($sql);
    }else{
        $sql = "insert into play(openid,thecode,jid,tiao,theday,createtime) values ('".$_SESSION['openid']."','".$code."','".$jId."','".$urd."','".$theday."','".date("Y-m-d H:i:s")."' )";
        $mysqli->query($sql);
    }
    
    $timestamp=time();
    if (1 != $jId && 2 != $jId){
        $jssdk = new JSSDK($appId, $secret);
        $acToken=$jssdk->getAccessToken();
        
        //$apiTicket=json_decode(file_get_contents('https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$acToken.'&type=wx_card'), true);
         
        //$api_ticket=$apiTicket['ticket'];
        
        
        $data = json_decode(file_get_contents("/var/www/dnsj/juan.json"));
        if ($data->expire_time < time()) {
            $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$acToken.'&type=wx_card';
            $res = json_decode(httpGet($url));
            $api_ticket = $res->ticket;
            if ($api_ticket) {
                $data->expire_time = time() + 600;
                $data->ticket = $api_ticket;
                $fp = fopen("/var/www/dnsj/juan.json", "w");
                fwrite($fp, json_encode($data));
                fclose($fp);
            }
        } else {
            $api_ticket = $data->ticket;
        }
        
        
        
        class Signature{
            function __construct(){
                $this->data = array();
            }
            function add_data($str){
                array_push($this->data, (string)$str);
            }
            function get_signature(){
                sort( $this->data, SORT_STRING );
                return sha1( implode( $this->data ) );
            }
        };
        
        
        $signature = new Signature();
        $signature->add_data($api_ticket);
        $signature->add_data($timestamp);
        $signature->add_data($cardId);
        $signature->add_data($code);
        $signature->add_data($openid);
        $sign=$signature->get_signature();
    }

    echo json_encode(array('j'=>$jId,'n'=>$jName[$jId]
        , 'c'=>array('cid'=>$cardId, 'code'=>$code, 'timestamp'=>$timestamp,'signature'=>$sign)));  
}


